<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=utf-8" 
import="java.util.*"
pageEncoding="utf-8"%>

<% String appName = request.getContextPath(); %>
<html >
<head>
 <meta charset="UTF-8">
<meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>用户管理</title>
</head>
<link rel="stylesheet" type="text/css" href="<%=appName %>/js/themes/metro-orange/easyui.css">
<script type="text/javascript" src="<%=appName %>/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=appName %>/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=appName %>/js/jquery.edatagrid.js"></script>
<link rel="stylesheet" type="text/css" href="<%=appName %>/js/themes/icon.css">
<body >
<div style="width:1600px;margin:100px auto">
    <div style="font-size:200%;text-align:center;">用户管理</div>

    <table id="task-list"  title="用户管理" style="width:1450px;height:700px"
            data-options="rownumbers:true,
            singleSelect:true,
            url:'<%=appName%>/mvc/security/getUsers',
            method:'get',
            onLoadError : loadErr,
            loadFilter : loadFilter,
            updateUrl:'<%=appName%>/mvc/security/modifyUser',
            saveUrl:'<%=appName%>/mvc/security/modifyUser',
            destroyUrl:'<%=appName%>/mvc/security/delUser',
            autoSave:true,
            onSuccess:onSuccess,
            onDestroy:onDestory,
            onRowContextMenu: showMenu,
            toolbar:'#tb'">
        <thead>
            <tr>
                <th data-options="field:'USERNAME',width:150,editor:{type:'textbox',options:{required:true, missingMessage:'用户名必填!'}}">用户名</th>
                <th data-options="field:'NICKNAME',width:150,editor:{type:'textbox',options:{required:true, missingMessage:'用户简单必填!'}}">简称</th>
                <th data-options="field:'ROLEID',width:150,formatter:roleFormatter, editor: { type: 'combobox', options: { url:'<%=appName%>/mvc/security/getRolesSimple', valueField: 'ID', textField: 'ROLENAME',multiple:true } }">用户角色</th>
                <th data-options="field:'DESCRIBE',width:350,editor:{type:'textbox'}">描述</th>
                <th data-options="field:'CREATEDT',width:180,align:'center'">创建时间</th>
                <th data-options="field:'CREATOR',width:180,align:'center'">创建者</th>
                <th data-options="field:'LASTUPDATE',width:180,align:'center'">最后修改时间</th>
            </tr>
        </thead>
    </table>
    <div id="tb" style="padding:2px 5px;">
        <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="refresh()" style="float:right;margin-right:20px">刷新</a>
    </div>
    <div id="mm" class="easyui-menu" style="width:120px;">
        <div onclick="addRow()" data-options="iconCls:'icon-add'">新增</div>
        <div onclick="editRow()" data-options="iconCls:'icon-edit'">编辑</div>
        <div class="menu-sep"></div>
        <div onclick="delRow()" data-options="iconCls:'icon-cancel'">删除</div>
        <div class="menu-sep"></div>
        <div onclick="refresh()" data-options="iconCls:'icon-reload'">刷新</div>
        <div class="menu-sep"></div>
        <div onclick="resetPasswd()" data-options="iconCls:'icon-resetpwd'">重置用户密码</div>
    </div>
 </div>
 <script type="text/javascript">

var clickedRow = undefined;
$.extend($.fn.datagrid.methods, {    
    addEditor : function(jq, param) {   
        if (param instanceof Array) {   
            $.each(param, function(index, item) {  
                var e = $(jq).datagrid('getColumnOption', item.field); 
                e.editor = item.editor; }); 
            } else {    
                var e = $(jq).datagrid('getColumnOption', param.field);    
                e.editor = param.editor;    
            }   
        },  
    removeEditor : function(jq, param) {    
        if (param instanceof Array) {   
            $.each(param, function(index, item) {  
                var e = $(jq).datagrid('getColumnOption', item);   
                e.editor = {};  
                }); 
        } else {    
            var e = $(jq).datagrid('getColumnOption', param);
            e.editor = {};  
        }   
    }
});
$(function(){
    $('#task-list').edatagrid({
    });
});

function onDestory(index,row){
    $.messager.show({
        //title:'数据刷新完成',
        msg:row.USERNAME + ' 成功删除.',
        timeout:5000,
        showType:'slide'
    });
}

function onSuccess(index,row){
    var msg = JSON.parse(row.msg);
    if(msg.code == '0'){
        $.messager.show({
            //title:'数据刷新完成',
            msg:row.USERNAME + ' 成功保存.',
            timeout:5000,
            showType:'slide'
        });
        row.ID=msg.returnData.ID;
        row.USERNAME=msg.returnData.USERNAME;
        row.NICKNAME=msg.returnData.NICKNAME;
        row.ROLEID=msg.returnData.ROLEID;
        row.ROLES=msg.returnData.ROLES;
        row.DESCRIBE=msg.returnData.DESCRIBE;
        row.CREATEDT=msg.returnData.CREATEDT;
        row.CREATOR=msg.returnData.CREATOR;
        row.LASTUPDATE=msg.returnData.LASTUPDATE;
        $('#task-list').datagrid('refreshRow',index);
        
    }else{
        $.messager.alert('Warning',msg.msg);
    }
}
 function showMenu(e,index,row){
    clickedRow = index;
    if(index == -1 ) return;
        e.preventDefault();
        $('#mm').menu('show', {
            left: e.pageX,
            top: e.pageY
        });
    }

 function delRow(){
     $('#task-list').edatagrid('destroyRow', clickedRow)
}
 function addRow(){
     if(clickedRow == -1){
         $('#task-list').edatagrid('addRow', clickedRow + 1);
     }
     $('#task-list').edatagrid('addRow')
 }
function refresh(){
    $('#task-list').datagrid('reload');
}

function resetPasswd(){
	if(clickedRow != -1){
        $('#task-list').datagrid('selectRow', clickedRow)
    }else{
    	return;
    }
           $('#task-list').datagrid('selectRow', clickedRow);
           var row = $('#task-list').datagrid('getSelected');
           
           $.messager.confirm('My Title', '你确定要重置用户 ' + row.USERNAME + " 的密码吗？", function(r){
               if (r){
                   $.ajax({
                       url: "<%=appName%>/mvc/security/resetPasswd",
                       type: "POST",
                       dataType: "json",
                       data: {
                           userName: row.USERNAME
                       },
                       success: function(data){
                           if(data.code=="0"){
                               $.messager.alert('消息',"操作成功：" + row.USERNAME + " 密码已被重置，新密码为 111111",'info');
                           } else {
                               $.messager.alert('错误',data.message,'error');
                           }
                       },
                       error: function(xml){
                           $.messager.alert('错误',"连接服务器失败！",'error');
                       }
                   });
               }
           });
}


function refresh(){
    
    var rows = $('#task-list').datagrid('getChanges');
    if(rows.length > 0){
        $.messager.confirm('警  告', '你有' + rows.length + '条记录变更后未保存，执行刷新操作后，变更将会丢失。取消刷新回去保存变更?', function(r){
            if (!r){
                return;
            }else{
                $('#task-list').datagrid('reload');
            }
        });
    }else{
        $('#task-list').datagrid('reload');
    }
}
function loadFilter(data){
    if(data.code == '0') {
        $.messager.show({
            //title:'数据刷新完成',
            msg:'数据刷新完成.',
            timeout:5000,
            showType:'slide'
        });
        return data.data;
    }
    $.messager.alert('错误',data.message,'error');
    return null;
}

function roleFormatter(value,row){
    return row.ROLES;
}




function loadErr(){
    $.messager.alert('错误',"loaderr",'error');
}
</script>
</body>

</html>